package org.linjie.generate.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

/**
 * 按行读取Txt文本文件
 */
public class ReadTextByLineUtils {

    private static Logger logger = LoggerFactory.getLogger(ReadTextByLineUtils.class);

    /**
     * 读取基于类根目录路径的文本文件
     *
     * @param localFilePath
     * @return
     */
    public static Map<String, String> readByLocalResource(String localFilePath) {
        Map<String, String> result = new HashMap<>();
        try (BufferedReader br = new BufferedReader(new InputStreamReader(ReadTextByLineUtils.class.getClassLoader().getResourceAsStream(localFilePath)))) {
            for (String buf; (buf = br.readLine()) != null; ) {
                if ("".equals(buf.trim())) {
                    continue;
                }
                result.put(buf, buf);
            }
        } catch (IOException e) {
            logger.error("读取文件报错", e);
            e.printStackTrace();
        }
        return result;
    }

    public static void main(String[] args) {
        Map<String, String> result = readByLocalResource("keywords.txt");
        System.out.println(result);
    }

}
